﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing.Drawing2D;
using System.Drawing;

namespace Maydew.Windows.Forms.Controls.FlexiCell
{
    public partial class FlexiCellControl
    {
        #region " Left Border "
        /// <summary>
        /// 设置选中单元格左侧边框样式。
        /// </summary>
        /// <param name="dashStyle">线条样式。</param>
        /// <param name="width">宽度。</param>
        /// <param name="color">颜色。</param>
        /// <param name="brush">自定义笔刷。</param>
        public void SetLeftBorderStyle(DashStyle dashStyle, float width, Color color, Brush brush)
        {
            int srow, erow;
            string scol, ecol;
            GetCellRectangle(_focusedCells, out srow, out scol, out erow, out ecol);
            SetLeftBorderStyle(srow, scol, erow, ecol, dashStyle, width, color, brush);
        }
        /// <summary>
        /// 设置指定单元格左侧边框样式。
        /// </summary>
        /// <param name="startRowIndex">开始行。</param>
        /// <param name="startColumnName">开始列。</param>
        /// <param name="endRowIndex">结束行。</param>
        /// <param name="endColumnName">结束列。</param>
        /// <param name="dashStyle">线条样式。</param>
        /// <param name="width">宽度。</param>
        /// <param name="color">颜色。</param>
        /// <param name="brush">自定义笔刷。</param>
        public void SetLeftBorderStyle(int startRowIndex, string startColumnName, int endRowIndex, string endColumnName,
            DashStyle dashStyle, float width, Color color, Brush brush)
        {
            int startColumnIndex = _columns.IndexOf(startColumnName);
            int endColumnIndex = _columns.IndexOf(endColumnName);
            int[] result = Swap(startRowIndex, endRowIndex);
            startRowIndex = result[0];
            endRowIndex = result[1];
            result = Swap(startColumnIndex, endColumnIndex);
            startColumnIndex = result[0];
            endColumnIndex = result[1];
            for (int r = startRowIndex; r <= endRowIndex; r++)
            {
                SetCellBorderStyle(_cells[r, _columns[startColumnIndex].ColumnName].LeftBorderStyle, dashStyle, width, color, brush);
            }
            if (startColumnIndex > 0)
            {
                startColumnIndex--;
                for (int r = startRowIndex; r <= endRowIndex; r++)
                {
                    SetCellBorderStyle(_cells[r, _columns[startColumnIndex].ColumnName].RightBorderStyle, dashStyle, width, color, brush);
                }
            }
        }
        #endregion

        #region " Top Border "
        /// <summary>
        /// 设置选中单元格上侧边框样式。
        /// </summary>
        /// <param name="dashStyle">线条样式。</param>
        /// <param name="width">宽度。</param>
        /// <param name="color">颜色。</param>
        /// <param name="brush">自定义笔刷。</param>
        public void SetTopBorderStyle(DashStyle dashStyle, float width, Color color, Brush brush)
        {
            int srow, erow;
            string scol, ecol;
            GetCellRectangle(_focusedCells, out srow, out scol, out erow, out ecol);
            SetTopBorderStyle(srow, scol, erow, ecol, dashStyle, width, color, brush);
        }
        /// <summary>
        /// 设置指定单元格上侧边框样式。
        /// </summary>
        /// <param name="startRowIndex">开始行。</param>
        /// <param name="startColumnName">开始列。</param>
        /// <param name="endRowIndex">结束行。</param>
        /// <param name="endColumnName">结束列。</param>
        /// <param name="dashStyle">线条样式。</param>
        /// <param name="width">宽度。</param>
        /// <param name="color">颜色。</param>
        /// <param name="brush">自定义笔刷。</param>
        public void SetTopBorderStyle(int startRowIndex, string startColumnName, int endRowIndex, string endColumnName,
            DashStyle dashStyle, float width, Color color, Brush brush)
        {
            int startColumnIndex = _columns.IndexOf(startColumnName);
            int endColumnIndex = _columns.IndexOf(endColumnName);
            int[] result = Swap(startRowIndex, endRowIndex);
            startRowIndex = result[0];
            endRowIndex = result[1];
            result = Swap(startColumnIndex, endColumnIndex);
            startColumnIndex = result[0];
            endColumnIndex = result[1];
            for (int c = startColumnIndex; c <= endColumnIndex; c++)
            {
                SetCellBorderStyle(_cells[startRowIndex, _columns[c].ColumnName].TopBorderStyle, dashStyle, width, color, brush);
            }
            if (startRowIndex > 0)
            {
                startRowIndex--;
                for (int c = startColumnIndex; c <= endColumnIndex; c++)
                {
                    SetCellBorderStyle(_cells[startRowIndex, _columns[c].ColumnName].BottomBorderStyle, dashStyle, width, color, brush);
                }
            }
        }
        #endregion

        #region " Right Border "
        /// <summary>
        /// 设置选中单元格右侧边框样式。
        /// </summary>
        /// <param name="dashStyle">线条样式。</param>
        /// <param name="width">宽度。</param>
        /// <param name="color">颜色。</param>
        /// <param name="brush">自定义笔刷。</param>
        public void SetRightBorderStyle(DashStyle dashStyle, float width, Color color, Brush brush)
        {
            int srow, erow;
            string scol, ecol;
            GetCellRectangle(_focusedCells, out srow, out scol, out erow, out ecol);
            SetRightBorderStyle(srow, scol, erow, ecol, dashStyle, width, color, brush);
        }
        /// <summary>
        /// 设置指定单元格右侧边框样式。
        /// </summary>
        /// <param name="startRowIndex">开始行。</param>
        /// <param name="startColumnName">开始列。</param>
        /// <param name="endRowIndex">结束行。</param>
        /// <param name="endColumnName">结束列。</param>
        /// <param name="dashStyle">线条样式。</param>
        /// <param name="width">宽度。</param>
        /// <param name="color">颜色。</param>
        /// <param name="brush">自定义笔刷。</param>
        public void SetRightBorderStyle(int startRowIndex, string startColumnName, int endRowIndex, string endColumnName,
            DashStyle dashStyle, float width, Color color, Brush brush)
        {
            int startColumnIndex = _columns.IndexOf(startColumnName);
            int endColumnIndex = _columns.IndexOf(endColumnName);
            int[] result = Swap(startRowIndex, endRowIndex);
            startRowIndex = result[0];
            endRowIndex = result[1];
            result = Swap(startColumnIndex, endColumnIndex);
            startColumnIndex = result[0];
            endColumnIndex = result[1];
            for (int r = startRowIndex; r <= endRowIndex; r++)
            {
                SetCellBorderStyle(_cells[r, _columns[endColumnIndex].ColumnName].RightBorderStyle, dashStyle, width, color, brush);
            }
            if (endColumnIndex < ColumnCount - 1)
            {
                endColumnIndex++;
                for (int r = startRowIndex; r <= endRowIndex; r++)
                {
                    SetCellBorderStyle(_cells[r, _columns[endColumnIndex].ColumnName].LeftBorderStyle, dashStyle, width, color, brush);
                }
            }
        }
        #endregion

        #region " Bottom Border "
        /// <summary>
        /// 设置选中单元格下侧边框样式。
        /// </summary>
        /// <param name="dashStyle">线条样式。</param>
        /// <param name="width">宽度。</param>
        /// <param name="color">颜色。</param>
        /// <param name="brush">自定义笔刷。</param>
        public void SetBottomBorderStyle(DashStyle dashStyle, float width, Color color, Brush brush)
        {
            int srow, erow;
            string scol, ecol;
            GetCellRectangle(_focusedCells, out srow, out scol, out erow, out ecol);
            SetBottomBorderStyle(srow, scol, erow, ecol, dashStyle, width, color, brush);
        }
        /// <summary>
        /// 设置指定单元格下侧边框样式。
        /// </summary>
        /// <param name="startRowIndex">开始行。</param>
        /// <param name="startColumnName">开始列。</param>
        /// <param name="endRowIndex">结束行。</param>
        /// <param name="endColumnName">结束列。</param>
        /// <param name="dashStyle">线条样式。</param>
        /// <param name="width">宽度。</param>
        /// <param name="color">颜色。</param>
        /// <param name="brush">自定义笔刷。</param>
        public void SetBottomBorderStyle(int startRowIndex, string startColumnName, int endRowIndex, string endColumnName,
            DashStyle dashStyle, float width, Color color, Brush brush)
        {
            int startColumnIndex = _columns.IndexOf(startColumnName);
            int endColumnIndex = _columns.IndexOf(endColumnName);
            int[] result = Swap(startRowIndex, endRowIndex);
            startRowIndex = result[0];
            endRowIndex = result[1];
            result = Swap(startColumnIndex, endColumnIndex);
            startColumnIndex = result[0];
            endColumnIndex = result[1];
            for (int c = startColumnIndex; c <= endColumnIndex; c++)
            {
                SetCellBorderStyle(_cells[endRowIndex, _columns[c].ColumnName].BottomBorderStyle, dashStyle, width, color, brush);
            }
            if (endRowIndex < RowCount - 1)
            {
                endRowIndex++;
                for (int c = startColumnIndex; c <= endColumnIndex; c++)
                {
                    SetCellBorderStyle(_cells[endRowIndex, _columns[c].ColumnName].TopBorderStyle, dashStyle, width, color, brush);
                }
            }
        }
        #endregion

        #region " Horizontal Inner "
        /// <summary>
        /// 设置选中单元格水平边框样式。
        /// </summary>
        /// <param name="dashStyle">线条样式。</param>
        /// <param name="width">宽度。</param>
        /// <param name="color">颜色。</param>
        /// <param name="brush">自定义笔刷。</param>
        public void SetHorizontalInnerStyle(DashStyle dashStyle, float width, Color color, Brush brush)
        {
            int srow, erow;
            string scol, ecol;
            GetCellRectangle(_focusedCells, out srow, out scol, out erow, out ecol);
            SetHorizontalInnerStyle(srow, scol, erow, ecol, dashStyle, width, color, brush);
        }
        /// <summary>
        /// 设置指定单元格水平边框样式。
        /// </summary>
        /// <param name="startRowIndex">开始行。</param>
        /// <param name="startColumnName">开始列。</param>
        /// <param name="endRowIndex">结束行。</param>
        /// <param name="endColumnName">结束列。</param>
        /// <param name="dashStyle">线条样式。</param>
        /// <param name="width">宽度。</param>
        /// <param name="color">颜色。</param>
        /// <param name="brush">自定义笔刷。</param>
        public void SetHorizontalInnerStyle(int startRowIndex, string startColumnName, int endRowIndex, string endColumnName,
            DashStyle dashStyle, float width, Color color, Brush brush)
        {
            int startColumnIndex = _columns.IndexOf(startColumnName);
            int endColumnIndex = _columns.IndexOf(endColumnName);
            int[] result = Swap(startRowIndex, endRowIndex);
            startRowIndex = result[0];
            endRowIndex = result[1];
            result = Swap(startColumnIndex, endColumnIndex);
            startColumnIndex = result[0];
            endColumnIndex = result[1];
            for (int r = startRowIndex; r <= endRowIndex; r++)
            {
                for (int c = startColumnIndex; c <= endColumnIndex; c++)
                {
                    Cell cell = _cells[r, _columns[c].ColumnName];
                    if (r != startRowIndex)
                    {
                        SetCellBorderStyle(cell.TopBorderStyle, dashStyle, width, color, brush);
                    }
                    if (r != endRowIndex)
                    {
                        SetCellBorderStyle(cell.BottomBorderStyle, dashStyle, width, color, brush);
                    }
                }
            }
        }
        #endregion

        #region " Vertical Inner "
        /// <summary>
        /// 设置选中单元格垂直边框样式。
        /// </summary>
        /// <param name="dashStyle">线条样式。</param>
        /// <param name="width">宽度。</param>
        /// <param name="color">颜色。</param>
        /// <param name="brush">自定义笔刷。</param>
        public void SetVerticalInnerStyle(DashStyle dashStyle, float width, Color color, Brush brush)
        {
            int srow, erow;
            string scol, ecol;
            GetCellRectangle(_focusedCells, out srow, out scol, out erow, out ecol);
            SetVerticalInnerStyle(srow, scol, erow, ecol, dashStyle, width, color, brush);
        }
        /// <summary>
        /// 设置指定单元格垂直边框样式。
        /// </summary>
        /// <param name="startRowIndex">开始行。</param>
        /// <param name="startColumnName">开始列。</param>
        /// <param name="endRowIndex">结束行。</param>
        /// <param name="endColumnName">结束列。</param>
        /// <param name="dashStyle">线条样式。</param>
        /// <param name="width">宽度。</param>
        /// <param name="color">颜色。</param>
        /// <param name="brush">自定义笔刷。</param>
        public void SetVerticalInnerStyle(int startRowIndex, string startColumnName, int endRowIndex, string endColumnName,
            DashStyle dashStyle, float width, Color color, Brush brush)
        {
            int startColumnIndex = _columns.IndexOf(startColumnName);
            int endColumnIndex = _columns.IndexOf(endColumnName);
            int[] result = Swap(startRowIndex, endRowIndex);
            startRowIndex = result[0];
            endRowIndex = result[1];
            result = Swap(startColumnIndex, endColumnIndex);
            startColumnIndex = result[0];
            endColumnIndex = result[1];
            for (int r = startRowIndex; r <= endRowIndex; r++)
            {
                for (int c = startColumnIndex; c <= endColumnIndex; c++)
                {
                    Cell cell = _cells[r, _columns[c].ColumnName];
                    if (c != startColumnIndex)
                    {
                        SetCellBorderStyle(cell.LeftBorderStyle, dashStyle, width, color, brush);
                    }
                    if (c != endColumnIndex)
                    {
                        SetCellBorderStyle(cell.RightBorderStyle, dashStyle, width, color, brush);
                    }
                }
            }
        }
        #endregion

        #region " 设置单元格样式 "
        /// <summary>
        /// 设置单元格边框样式。
        /// </summary>
        /// <param name="borderStyle">边框样式。</param>
        /// <param name="dashStyle">线条样式。</param>
        /// <param name="width">宽度。</param>
        /// <param name="color">颜色。</param>
        /// <param name="brush">自定义笔刷。</param>
        protected virtual void SetCellBorderStyle(CellBorderStyle borderStyle,DashStyle dashStyle, float width, Color color, Brush brush)
        {
            borderStyle.DashStyle = dashStyle;
            borderStyle.Width = width;
            borderStyle.Color = color;
            borderStyle.Brush = brush;

            base.Invalidate();
        }
        #endregion

        #region " 私有方法 "
        /// <summary>
        /// 交换两个参数值。
        /// </summary>
        /// <param name="x1">参数1。</param>
        /// <param name="x2">参数2。</param>
        /// <returns>返回互换后的值。</returns>
        private int[] Swap(int x1, int x2)
        {
            int[] result = new int[2];
            if (x1 > x2)
            {
                int temp = x1;
                x1 = x2;
                x2 = temp;
            }
            result[0] = x1;
            result[1] = x2;
            return result;
        }
        #endregion
    }
}
